package com.phpxiu.app.adapter.holder;
import android.net.Uri;
import android.view.View;
import com.phpxiu.app.model.msg.C2CImageMsg;
import com.phpxiu.app.model.msg.C2CMsg;
import com.phpxiu.app.model.msg.C2CTextMsg;
import com.phpxiu.app.model.msg.C2CVoiceMsg;
import com.phpxiu.app.utils.FileManager;
import com.phpxiu.app.utils.KKYUtil;
import com.tencent.TIMImage;
import com.tencent.TIMImageElem;
import com.tencent.TIMImageType;
import com.tencent.TIMMessage;
import com.tencent.TIMSoundElem;
import com.tencent.TIMTextElem;
import com.tencent.TIMValueCallBack;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * 私信聊天消息
 * Created by 吴金洲 on 2016/7/28
 */
public class C2CChatMsgViewHolder extends C2CChatMyMsgViewHolder{
    private boolean isCanPreviewImage=true;//可以预览图片，如果发的是原始图片则可以预览
    private String url;
    public C2CChatMsgViewHolder(View itemView) {
        super(itemView);
    }

    /**
     * 显示消息
     * @param msg
     */
    public void showMsg(C2CMsg msg){
        c2cMsg=msg;
        showMsgTime(msg);//检测是否显示消息时间
        if(msg.isSelf()){
            playAnim=myFrameAnimation;
            showMyMsg(msg);
        }else{
            playAnim=friendFrameAnimation;
            //显示好友信息
            showFriendMsg(msg);
        }
    }

    /**
     * 显示好友的消息
     * @param msg
     */
    public void showFriendMsg(C2CMsg msg){
        //隐藏我的内容显示区域
        if(myView.getVisibility()==View.VISIBLE){
            myView.setVisibility(View.GONE);
        }
        //显示好友内容显示区域
        if(friendView.getVisibility()!=View.VISIBLE){
            friendView.setVisibility(View.VISIBLE);
        }
        if(msg instanceof C2CTextMsg){
            showFriendTextMsg(msg);//显示文本消息
        }
        if(msg instanceof C2CVoiceMsg){//显示语音消息
            showFriendVoiceMsg(msg);
        }
        if(msg instanceof C2CImageMsg){//显示图片消息
            showFriendImageMsg(msg);
        }
    }

    /**
     * 显示我发送的文本、表情消息
     * @param msg
     */
    private void showFriendTextMsg(C2CMsg msg){
        TXT.clear();
        //隐藏显示的语音消息内容
        if(friendVoiceView.getVisibility()==View.VISIBLE){
            friendVoiceView.setVisibility(View.GONE);
        }
        //隐藏显示的图片消息内容
        if(friendPicView.getVisibility()==View.VISIBLE){
            friendPicView.setVisibility(View.GONE);
        }
        if(friendMsgView.getVisibility()!=View.VISIBLE){
            friendMsgView.setVisibility(View.VISIBLE);
        }
        TIMMessage timMessage=msg.getMessage();
        TIMTextElem textElem=null;
        for (int i=0;i<timMessage.getElementCount();i++){
            textElem = (TIMTextElem) timMessage.getElement(i);
            TXT.append(textElem.getText());
        }
        friendMsgView.setText(TXT);
    }

    /**
     * 显示好友的语音消息
     * @param msg
     */
    private void showFriendVoiceMsg(C2CMsg msg){
        //隐藏显示的图片消息内容
        if(friendPicView.getVisibility()==View.VISIBLE){
            friendPicView.setVisibility(View.GONE);
        }
        if(friendMsgView.getVisibility()==View.VISIBLE){
            friendMsgView.setVisibility(View.GONE);
        }
        if(friendVoiceView.getVisibility()!=View.VISIBLE){
            friendVoiceView.setVisibility(View.VISIBLE);
        }
        TIMMessage message=msg.getMessage();
        TIMSoundElem voice=(TIMSoundElem) message.getElement(0);
        friendVoiceTimeView.setText(String.valueOf(voice.getDuration())+"’");
    }

    /**
     * 显示好友图片
     * @param msg
     */
    private void showFriendImageMsg(C2CMsg msg){
        if(friendMsgView.getVisibility()==View.VISIBLE){
            friendMsgView.setVisibility(View.GONE);
        }
        if(friendVoiceView.getVisibility()==View.VISIBLE){
            friendVoiceView.setVisibility(View.GONE);
        }
        if(friendPicView.getVisibility()!=View.VISIBLE){
            friendPicView.setVisibility(View.VISIBLE);
        }
        TIMMessage message=msg.getMessage();
        TIMImageElem elem = (TIMImageElem) message.getElement(0);
        if(elem.getLevel()==0||elem.getLevel()==2){
            friendPicView.setOnClickListener(this);
        }else{
            friendPicView.setOnClickListener(null);
        }
        KKYUtil.log("好友图片级别："+elem.getLevel());//level - 0: 原图发送 1: 高压缩率图发送(图片较小，默认值) 2:高清图发送(图片较大)
        List<TIMImage>images=elem.getImageList();
        int size=images.size();
        if(size>0){
            url=images.get(size-1).getUrl();
        }
        for(final TIMImage image : images) {
            KKYUtil.log("好友图片："+image.getUrl()+"@@"+elem.getImageFormat());
            if (image.getType() == TIMImageType.Thumb){
                final String uuid = image.getUuid();
                long w=image.getWidth();
                long h=image.getHeight();
                KKYUtil.log("好友图片宽高："+image.getWidth()+"x"+image.getHeight());
                rParams.width=(int)Math.min(w,maxPicWith);
                rParams.height=(int)Math.min(h,maxPicHeight);
                friendPicView.setLayoutParams(rParams);
                if (FileManager.isCacheFileExist(uuid)){
                    showThumb(uuid);
                }else{
                    image.getImage(new TIMValueCallBack<byte[]>() {
                        @Override
                        public void onError(int code, String desc) {//获取图片失败
                        }

                        @Override
                        public void onSuccess(byte[] data) {//成功，参数为图片数据
                            FileManager.createFile(data, uuid);
                            showThumb(uuid);
                        }
                    });
                }
            }
            if (image.getType() == TIMImageType.Original){
                final String uuid = image.getUuid();
            }
        }
    }

    private void showThumb(String fileName){
        friendPicView.setTag(url);
        friendPicView.setImageURI(Uri.parse("file:///" + FileManager.getCacheFilePath(fileName)));
    }

}
